package com.copractice.data.entity.answer;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.copractice.data.entity.PersistentObject;
import com.copractice.data.entity.feedback.FeedbackAnswer;
import com.copractice.data.entity.question.Question;

/**
 * Represents answer entity.
 *
 * @author Dmitry Churbanov (dmitry.churbanov@gmail.com)
 * @version 1.0 03/19/2009
 */
@Entity
@Table(name = "ANSWERS")
@SuppressWarnings("serial")
public class Answer extends PersistentObject {

    /**
     * Answer's content
     */
    private String answer;
    
    /**
     * Defines, if answer right or not 
     */
    private Boolean right;

    /**
     * Question to which this answer belongs
     */
    private Question question;
    
    /**
     * list of feedback answers which contains all score data about this answer
     * and feedbacks which was written for the question  
     */
    private List<FeedbackAnswer> feedbackAnswer;      
    
    /**
     * Empty constructor
     */
    public Answer(){}
    
    /**
     * Constructor which accepts answer text and is it right or not
     * 
     * @param answer answer text
     * @param right determines if this answer is right or not. Should be true 
     *              if it is right, otherwise false
     */
    public Answer(String answer, boolean right) {
        this.answer = answer;
        this.right = right;
    }

    /**
     * @see edu.vt.intelligenttutor.data.entity.PersistentObject#getId()  
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Override
    public Long getId() {
        return super.getId();
    }

    /**
     * Returns answer content
     *
     * @return answer content
     */
    @Column(name = "ANSWER")
    public String getAnswer() {
        return answer;
    }

    /**
     * Sets answer content
     *
     * @param answer content of this answer
     */
    public void setAnswer(String answer) {
        this.answer = answer;
    }

    /**
     * Returns if answer is right for the question it is associated with or not
     *
     * @return true if this answer is right for the question it is associated with,
     *         otherwise returns false 
     */
    @Column(name = "IS_RIGHT")
    public Boolean getRight() {
        return right;
    }

    /**
     * Sets if answer is right for the question it is associated with or not
     *
     * @param right a <code>Boolean</code> which defines if answer is right for
     *              the question it is associated with or not
     */
    public void setRight(Boolean right) {
        this.right = right;
    }

    /**
     * Returns question this answer is associated with
     *
     * @return question this answer is associated with
     */
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    @JoinColumn(name="QUESTION_ID")
    public Question getQuestion() {
        return question;
    }

    /**
     * Sets the question this answer is associated with
     *
     * @param question a <code>Question</code> this answer is associated with
     */
    public void setQuestion(Question question) {
        this.question = question;
    }
    
    /**
     * Returns the list of feedback answers which contains all score data about 
     * this answer and feedbacks which was written for the question
     * 
     * @return list of <code>FeedbackAnswer</code>
     */
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "answer")
    public List<FeedbackAnswer> getFeedbackAnswer() {
        return feedbackAnswer;
    }

    /**
     * Sets the list of feedback answers which contains all score data about 
     * this answer and feedbacks which was written for the question
     * 
     * @param feedbackAnswer list of <code>FeedbackAnswer</code>
     */
    public void setFeedbackAnswer(List<FeedbackAnswer> feedbackAnswer) {
        this.feedbackAnswer = feedbackAnswer;
    }    
    
    /**
     * Returns a string representation of the entity.
     * 
     * @return a string representation of the entity.
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this)
                   .append("id", getId())
                   .append("answer", answer)
                   .append("right", right)
                   .append("question", question)
                   .toString();
    }     
    
}
